* Table C1: Applicants' choice set

********************************************************************************
************************** Prepare data ****************************************
********************************************************************************

do "${dir_code}/helpers/TableC1_helper.do"

********************************************************************************
************************** Prepare table ***************************************
********************************************************************************

cap file close sumstat
file open sumstat using "${tableC1}", write replace
file write sumstat "\begin{tabular}{l S[table-number-alignment = center, table-format = 5.0] S[table-number-alignment = center, table-format = 5.0] S[table-number-alignment = center, table-format = 5.0]} \\ \hline\hline" _n
file write sumstat " & \multicolumn{1}{c}{Total} & \multicolumn{1}{c}{Full-time} & \multicolumn{1}{c}{Part-time} \\ "
file write sumstat " & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} \\ \hline "
file write sumstat "\multicolumn{4}{l}{A. {\it Alternatives}} \\" _n

qui count if contract_counter == 1
local prog_num: display %12.0fc r(N)
forvalues i = 0/1 {
	qui count if contract_counter == 1 & fulltime == `i'
	local prog_num`i': display %12.0fc r(N)
}

file write sumstat "Number of programs & `prog_num' & `prog_num1' & `prog_num0' \\" _n

qui count if funding == 1 & contract_count == 2
local funded_and_unfunded: display %12.0fc r(N)
	
forvalues i = 0/1 {
	qui count if funding == 1 & contract_count == 2 & fulltime == `i'
	local funded_and_unfunded`i': display %12.0fc r(N)
} 

file write sumstat "\multicolumn{1}{l}{-- state- and self-funded} & `funded_and_unfunded' & `funded_and_unfunded1' & `funded_and_unfunded0' \\" _n

qui count if funding == 1 & contract_count == 1
local funded_only: display %12.0fc r(N)
forvalues i = 0/1 {
	qui count if funding == 1 & contract_count == 1 & fulltime == `i'
	local funded_only`i': display %12.0fc r(N)
} 

file write sumstat "-- state-funded exclusively & `funded_only' & `funded_only1'  & `funded_only0' \\" _n

qui count if funding == 2 & contract_count == 1
local unfunded_only: display %12.0fc r(N)
forvalues i = 0/1 {
	qui count if funding == 2 & contract_count == 1 & fulltime == `i'
	local unfunded_only`i': display %12.0fc r(N)
}
file write sumstat "-- self-funded exclusively & `unfunded_only' & `unfunded_only1'  & `unfunded_only0' \\" _n

qui count 
local contract_num: display %12.0fc r(N)

forvalues i = 0/1 {
	qui count if fulltime == `i'
	local contract_num`i': display %12.0fc r(N)
}

file write sumstat "Number of alternatives & `contract_num' & `contract_num1' & `contract_num0' \\" _n
file write sumstat "& & & \\" _n
file write sumstat "\multicolumn{4}{l}{B. {\it Capacities}} \\" _n

import delimited "${data_capacities_benchmark}", clear 
rename cid contract_id
merge 1:n contract_id using "${data_applications}"
bys contract_id: gen ss = _n
keep if ss == 1
keep contract_id program_id capacity funding munkarend admitted_applicants

gen fulltime = munkarend == 1
keep program_id contract_id capacity funding fulltime admitted_applicants
replace capacity = admitted_applicants if admitted_applicants > capacity

bys program_id: gen contract_count = _N
bys program_id: gen contract_counter = _n

qui sum capacity if contract_count == 2, detail
local funded_unfunded_sum: display %12.0fc r(sum)
forvalues i = 0/1 {
	qui sum capacity if contract_count == 2 & fulltime == `i', detail
	local funded_unfunded_sum`i': display %12.0fc r(sum)
}
file write sumstat "\multicolumn{1}{l}{State-funded and self-funded} & `funded_unfunded_sum' & `funded_unfunded_sum1' &  `funded_unfunded_sum0' \\" _n

qui sum capacity if contract_count == 2 & funding == 1, detail
local funded_unfunded1_sum: display %12.0fc r(sum)
forvalues i = 0/1 {
	qui sum capacity if contract_count == 2 & funding == 1 & fulltime == `i', detail
	local funded_unfunded1_sum`i': display %12.0fc r(sum)
}
file write sumstat " -- state-funded & `funded_unfunded1_sum' & `funded_unfunded1_sum1' &  `funded_unfunded1_sum0' \\" _n

qui sum capacity if contract_count == 2 & funding == 2, detail
local funded_unfunded2_sum: display %12.0fc r(sum)
forvalues i = 0/1 {
	qui sum capacity if contract_count == 2 & funding == 2 & fulltime == `i', detail
	local funded_unfunded2_sum`i': display %12.0fc r(sum)
}
file write sumstat " -- self-funded & `funded_unfunded2_sum' & `funded_unfunded2_sum1' &  `funded_unfunded2_sum0' \\" _n

qui sum capacity if contract_count == 1 & funding == 1, detail
local funded_only_sum: display %12.0fc r(sum)
forvalues i = 0/1 {
	qui sum capacity if contract_count == 1 & funding == 1 & fulltime == `i', detail
	local funded_only_sum`i': display %12.0fc r(sum)
}
file write sumstat "State-funded exclusively & `funded_only_sum' & `funded_only_sum1' &  `funded_only_sum0' \\" _n

qui sum capacity if contract_count == 1 & funding == 2, detail
local unfunded_only_sum: display %12.0fc r(sum)
forvalues i = 0/1 {
	qui sum capacity if contract_count == 1 & funding == 2 & fulltime == `i', detail
	local unfunded_only_sum`i': display %12.0fc r(sum)
}
file write sumstat "Self-funded exclusively & `unfunded_only_sum' & `unfunded_only_sum1' &  `unfunded_only_sum0' \\ \hline " _n

qui sum capacity, detail
local capacity_sum: display %12.0fc r(sum)
forvalues i = 0/1 {
	qui sum capacity if fulltime == `i', detail
	local capacity_sum`i': display %12.0fc r(sum)
}
file write sumstat "Total & `capacity_sum' & `capacity_sum1' &  `capacity_sum0' \\ \hline\hline " _n

file write sumstat "\multicolumn{4}{p{9.5cm}}{{\it Notes}: The table presents summary statistics of the applicants' choice set. Panel A presents the number of alternatives by the financial terms. Panel B displays the number of seats by the financial terms.}" _n
file write sumstat "\end{tabular}" _n
file close sumstat
